﻿using AutoMapper;
using PagedList;
using SIG.Data.Repositories;
using SIG.Infrastructure.Configs;
using SIG.Infrastructure.Helper;
using SIG.InputModel.Admin.LuceneSearch;
using SIG.Model.LuceneSearch;
using SIG.Resources.Admin;
using SIG.Service.LuceneSearch;
using SIG.ViewModel;
using SIG.ViewModel.Admin.LuceneSearch;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Xml.Linq;

namespace SIG.WebMVC.Areas.Admin.Controllers
{
    public class LuceneController : BaseController
    {
        private readonly IMapper _mapper;

        public LuceneController(IMapper mapper)
        {         
            _mapper = mapper;
        }
        public ActionResult Index(string searchTerm, string searchField, bool? searchDefault, int? page)
        {
            var vm = new IndexViewModel()
            {
                SearchTerm = searchTerm,
                SearchField = searchField,
                SearchDefault = searchDefault ?? false,
                PageIndex = (page ?? 1) - 1,
                PageSize = SettingsManager.Lucene.PageSize
            };
            
            // create default Lucene search index directory
            if (!Directory.Exists(GoLucene._luceneDir)) Directory.CreateDirectory(GoLucene._luceneDir);

            int totalCount = 0;
            // perform Lucene search
            List<SearchData> _searchResults;
            if (searchDefault == true)
                _searchResults = (string.IsNullOrEmpty(searchField)
                                   ? GoLucene.SearchDefault(vm.PageIndex, vm.PageSize, out totalCount,searchTerm)
                                   : GoLucene.SearchDefault(vm.PageIndex, vm.PageSize, out totalCount,searchTerm, searchField)).ToList();
            else
                _searchResults = (string.IsNullOrEmpty(searchField)
                                   ? GoLucene.Search(vm.PageIndex, vm.PageSize, out totalCount,searchTerm)
                                   : GoLucene.Search(vm.PageIndex, vm.PageSize, out totalCount,searchTerm, searchField)).ToList();
            if (string.IsNullOrEmpty(searchTerm) && !_searchResults.Any())
            {
                var pageList = GoLucene.GetAllIndexRecords().ToList();
                totalCount = pageList.Count();
                _searchResults = pageList.Skip(vm.PageIndex * vm.PageSize).Take(vm.PageSize).ToList();
            }
                


            // setup and return view model
            var search_field_list = new
                List<SelectedList> {
                                         new SelectedList {Text = "--所有字段--", Value = ""},                                         
                                         new SelectedList {Text = "标题", Value = "Name"},
                                         new SelectedList {Text = "网址", Value = "Name"},
                                         new SelectedList {Text = "内容", Value = "Description"}

                                     };


            vm.TotalCount = totalCount;
            vm.SearchIndexData = new StaticPagedList<SearchData>(_searchResults, vm.PageIndex + 1, vm.PageSize, vm.TotalCount);  ;
            vm.SearchFieldList = search_field_list;

            ViewBag.PageSizes = new SelectList(Site.PageSizes());

            return View(vm);
            
        }

        [HttpPost]
        public JsonResult PageSizeSet(int pageSize)
        {
            try
            {
                var xmlFile = Server.MapPath("~/Config/LuceneSettings.config");
                XDocument doc = XDocument.Load(xmlFile);

                var item = doc.Descendants("Settings").FirstOrDefault();
                item.Element("PageSize").SetValue(pageSize);
                doc.Save(xmlFile);

                return Json(AR, JsonRequestBehavior.DenyGet);
            }
            catch (Exception ex)
            {
                AR.Setfailure(ex.Message);
                return Json(AR, JsonRequestBehavior.DenyGet);
            }
        }

        //public ActionResult Search(string searchTerm, string searchField, string searchDefault)
        //{
        //    return RedirectToAction("Index", new { searchTerm, searchField, searchDefault });
        //}

        //public ActionResult CreateIndex()
        //{
        //    GoLucene.AddUpdateLuceneIndex(SampleDataRepository.GetAll());
        //    TempData["Result"] = "Search index was created successfully!";
        //    return RedirectToAction("Index");
        //}


        [HttpGet]
        public PartialViewResult AddToIndex()
        {
            
            return PartialView("_AddToIndex");
        }

        [HttpPost]
        public ActionResult AddToIndex(SearchDataIM sampleData)
        {
            if (!ModelState.IsValid)
            {
                AR.Setfailure(GetModelErrorMessage());
                return Json(AR, JsonRequestBehavior.DenyGet);
            }

            var im = _mapper.Map<SearchData>(sampleData);
            GoLucene.AddUpdateLuceneIndex(im);

            AR.SetSuccess(String.Format(Messages.AlertCreateSuccess,"索引目录"));
            return Json(AR, JsonRequestBehavior.DenyGet);

           
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public JsonResult ClearIndex()
        {
            if (GoLucene.ClearLuceneIndex())
            {
                AR.SetSuccess(String.Format(Messages.AlertDeleteSuccess, "索引"));
                return Json(AR, JsonRequestBehavior.DenyGet);
            }

            else
            {
                AR.Setfailure("索引被锁，不能被清除，迟点再试！");
                return Json(AR, JsonRequestBehavior.DenyGet);
            }
              
        }
        [HttpPost]
        [ValidateAntiForgeryToken]
        public JsonResult ClearIndexRecord(string id)
        {
            try
            {
                GoLucene.ClearLuceneIndexRecord(id);
               
                AR.SetSuccess(String.Format(Messages.AlertDeleteSuccess, "索引"));
                return Json(AR, JsonRequestBehavior.DenyGet);
            }catch(Exception er)
            {
                AR.Setfailure(er.Message);
                return Json(AR, JsonRequestBehavior.DenyGet);
            }
           
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public JsonResult OptimizeIndex()
        {

            try
            {
                GoLucene.Optimize();
                AR.SetSuccess(Messages.AlertActionSuccess);
                return Json(AR, JsonRequestBehavior.DenyGet);
            }
            catch (Exception er)
            {
                AR.Setfailure(er.Message);
                return Json(AR, JsonRequestBehavior.DenyGet);
            }
           
        }

    }
}